package Servlet.manager;

import Dao.UserDao;
import Manage.Manager;
import Manage.User;
import Manage.User_login;
import Utils.mybatisUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.io.PrintWriter;

public class maninsertServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user=request.getParameter("user");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String sex=request.getParameter("sex");
        String phone=request.getParameter("phone");
        String address=request.getParameter("address");
        String num=request.getParameter("num");
        Manager manager=new Manager(null,username,phone,address,user,Integer.parseInt(num),sex,password);
        PrintWriter pw =response.getWriter();
        SqlSession session= mybatisUtils.getSqlSession();
        UserDao dao=session.getMapper(UserDao.class);
        Integer judge=dao.superinsert(manager);
        User user1=new User();
        user1.setNum(Integer.parseInt(num));
        user1.setSex(sex);
        user1.setPhone(phone);
        user1.setAddress(address);
        user1.setUsername(username);
        if (judge!=0) {
            dao.register(user,username,password);
            dao.usersinsert(user1);
            pw.print("操作成功");
        }
        session.commit();
        pw.flush();
        pw.close();
        session.close();
    }
}
